package com.moon.game.command.guest.player;

import com.moon.game.clazz.PlayerItemsProto.PlayerItems.PlayerItem;
import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.exception.GException;
import com.moon.game.service.PlayerItemService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.PlayerVo;

/**
 * @description 穿上装备
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-24 上午10:59:25
 */
public class WearTheEquipCmd extends BaseCmd{
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		long playerItemId = Long.parseLong(ps[0]);
		PlayerItem playerItem = PlayerItemService.getPlayerItem(req.getPlayerId(), playerItemId);
		StringBuffer info = new StringBuffer();
		PlayerVo player = getPlayerVo(req);
		if (playerItem == null) {
			info.append("您的装备已经不存在！");
		}else {
			if (player.getLevel() < playerItem.getLevel()) {
				info.append("等级不够,装配失败!");
			}else if (player.getCharmValue() < playerItem.getNeedCharmValue()) {
				info.append("魅力不够,装配失败!");
			}else {
//				PlayerItem.newBuilder(playerItem).setIsUse(true);
				int ret = PlayerService.wearTheEquip(player, playerItem);
				if (ret == 0) {
					PlayerItemService.syncPlayerItem(req.getPlayerId(), PlayerItem.newBuilder(playerItem).setIsUse(true).build());
//					UpdatePlayerThread.addSyncPlayerMap(player);
					PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
					info.append("装配成功!");
				}else if (ret == -1) {
					info.append("装备的位置已经有道具了，请先卸下对应位置的装备！");
				}else if (ret == -2) {
					info.append("要装备的道具是坏的，请先修复！");
				}else if (ret == -3) {
					info.append("要装备的道具持久太低，请先修理！");
				}
			}
		}
		req.setVO(PAR_PLAYER_VO, player);
		req.setVO(TAG_INFO, info);
		response(req, req.getClientResponse());
	}

}
